//
//  WMEClubCommentsTableViewCell.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/27.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEClubCommentsTableViewCell.h"
#import <UIImageView+WebCache.h>


@interface WMEClubCommentsTableViewCell ()
@property (nonatomic, retain)UIImageView *userImg;/**<用户头像*/
@property (nonatomic, retain)UILabel *userName;/**<用户名*/
@property (nonatomic, retain)UILabel *content;/**<内容*/
@property (nonatomic, retain)UILabel *time;/**<距离现在何时*/
@property (nonatomic, retain)UILabel *line;/**<用来美观的线*/
@end

@implementation WMEClubCommentsTableViewCell
- (void)dealloc
{
    [_model release];
    [_userImg release];
    [_userName release];
    [_time release];
    [_line release];
    [super dealloc];
}
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //用户头像
        self.userImg = [[UIImageView alloc] init];
//        _userImg.backgroundColor = [UIColor redColor];
        _userImg.layer.cornerRadius = 15;
        _userImg.contentMode = UIViewContentModeScaleAspectFill;
        _userImg.clipsToBounds = YES;
        _userImg.userInteractionEnabled = YES;
        UITapGestureRecognizer *userImgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushUserAction:)];
        [_userImg addGestureRecognizer:userImgTap];
        [self.contentView addSubview:_userImg];
        [_userImg release];
        [userImgTap release];
        
        //用户名
        self.userName = [[UILabel alloc] init];
//        _userName.backgroundColor = [UIColor orangeColor];
        _userName.textColor = [UIColor colorWithRed:0 green:160/255.0 blue:233/255.0 alpha:1];
        _userName.font = [UIFont boldSystemFontOfSize:11];
        _userName.userInteractionEnabled = YES;
        UITapGestureRecognizer *userNameTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushUserAction:)];
        [_userName addGestureRecognizer:userNameTap];
        [self.contentView addSubview:_userName];
        [_userName release];
        [userNameTap release];
        
        //主要内容
        self.content = [[UILabel alloc] init];
        _content.font = [UIFont systemFontOfSize:11];
        _content.numberOfLines = 0;
//        _content.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_content];
        [_content release];
        
        //距离现在何时
        self.time = [[UILabel alloc] init];
//        _time.backgroundColor = [UIColor blueColor];
        _time.textColor = [UIColor lightGrayColor];
        _time.font = [UIFont systemFontOfSize:9];
        _time.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_time];
        [_time release];
        
        //用来美化的线
        self.line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_line];
        [_line release];
    }
    return self;
}
#pragma mark - 重写setting方法
- (void)setModel:(WMEClubInformationModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        [_userImg sd_setImageWithURL:[NSURL URLWithString:_model.senderAvatar]];
        
        _userName.text = _model.senderName;
        
        _content.text = _model.content;
        //调用 计算距离现在多长时间的方法 来给time赋值
        _time.text = [[self class] compareCurrentTime:_model.add_datetime_ts];
        
    }
}
#pragma mark - 布局函数
- (void)layoutSubviews
{
    [super layoutSubviews];
    _userImg.frame = CGRectMake(60, 10, 30, 30);
    
    _userName.frame = CGRectMake(100, 15, WIDTH - 150, 15);
    
    CGFloat contentHeight = [[self class] heightWithText:_model.content FontOfSize:11];
    _content.frame = CGRectMake(100, 40, WIDTH - 110, contentHeight);
    
    _time.frame = CGRectMake(WIDTH - 100, 15, 90, 20);
    
    _line.frame = CGRectMake(60, _content.frame.size.height + _content.frame.origin.y + 10, WIDTH - 90, 0.3);
}
#pragma mark - 计算文本高
//计算文本高
+ (CGFloat)heightWithText:(NSString *)text FontOfSize:(CGFloat )fontOfSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontOfSize]};
    //最大范围
    CGSize size = CGSizeMake(WIDTH - 110, 1000);
    //计算 文本 需的rectangular
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回文本 的 高
    return r.size.height;
}

#pragma mark - 计算文本宽
//计算文本宽
+ (CGFloat)widthWithText:(NSString *)text MySize:(CGSize)size MyFontSize:(CGFloat)fontSize
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    //    CGSize size = CGSizeMake(WIDTH - 100, 10);
    CGRect r = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return r.size.width;
}

#pragma mark - 距离现在多久
//计算时间 转换成 距离现在 多久
+ (NSString *)compareCurrentTime:(NSNumber *)date
{
    // 返回以1970/01/01 GMT为基准，然后过了secs秒的时间
    NSDate *compareDate = [NSDate dateWithTimeIntervalSince1970:[date doubleValue]];
    // 以当前时间(Now)为基准时间，返回实例保存的时间与当前时间(Now)的时间间隔
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval / 60) < 60){
        result = [NSString stringWithFormat:@"%ld分钟前", temp];
    }
    
    else if((temp = temp / 60) < 24){
        result = [NSString stringWithFormat:@"%ld小时前", temp];
    }
    
    else if((temp = temp / 24) < 30){
        result = [NSString stringWithFormat:@"%ld天前", temp];
    }
    
    else if((temp = temp / 30) < 12){
        result = [NSString stringWithFormat:@"%ld月前", temp];
    }
    else{
        temp = temp / 12;
        result = [NSString stringWithFormat:@"%ld年前", temp];
    }
    
    return  result;
}
#pragma mark - 手势行动
- (void)pushUserAction:(UITapGestureRecognizer *)tap
{
    //调用协议 跳转到用户界面
    [self.delegate pushUserAction:_model.senderId.stringValue];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
